﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.Api.Biz
{
    public class OAuthService : ApiService
    {
        /// <summary>
        /// 用户登录
        /// </summary>
        public void Login(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string mobile = dic.GetValue<string>("mobile");
            string code = dic.GetValue<string>("code");

            ////20190326临时测试一下
            //mobile = "17721110000";
            //code = "666666";
            //SmsBiz.Instance.SendMessage(mobile, 1);

            if (string.IsNullOrEmpty(mobile) || string.IsNullOrEmpty(code))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //检查手机号是否合法
            if (!Utils.CheckMobile(mobile))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "手机号码错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //检查验证码是否合法
            if (!Utils.CheckMobileCode(code))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "验证码6位数字";
                apiContext.apiHttpResp = apiResponse;
                return;
            }


            //执行用户登录方法
            Tuple<bool, AccLoginModel, string> retTuple = AccountBiz.Instance.Login(mobile, code, apiContext.apiHttpReq.machineCode);
            if (retTuple.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(retTuple.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = retTuple.Item3;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 微信登录
        /// </summary>
        /// <param name="apiContext"></param>
        public void WxLogin(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string code = dic.GetValue<string>("code");     //app微信授权登入获取微信返回的code,用于获取微信access_token
            if (string.IsNullOrEmpty(code))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //执行用户登录方法
            Tuple<bool, WxAccLoginModel, string> retTuple = AccountBiz.Instance.WxLogin(code, apiContext.apiHttpReq.machineCode);
            if (retTuple.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(retTuple.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = retTuple.Item3;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 账号绑定
        /// </summary>
        public void Bind(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();
            string data = apiContext.decData;

            //解密后的json字符串反序列成对象
            SortedDictionary<string, object> dic = JsonHelper.DeserializeLower(data);
            string mobile = dic.GetValue<string>("mobile");     //手机号
            string code = dic.GetValue<string>("code");         //验证码
            string openid = dic.GetValue<string>("openid");     //微信授权openid
            string accesstoken = dic.GetValue<string>("accesstoken");

            if (string.IsNullOrEmpty(mobile) || string.IsNullOrEmpty(code) || string.IsNullOrEmpty(openid) || string.IsNullOrEmpty(accesstoken))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "缺少参数";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //检查手机号是否合法
            if (!Utils.CheckMobile(mobile))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "手机号码错误";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            //检查验证码是否合法
            if (!Utils.CheckMobileCode(code))
            {
                apiResponse.retCode = ResultEnum.ParamError;
                apiResponse.message = "验证码6位数字";
                apiContext.apiHttpResp = apiResponse;
                return;
            }

            Tuple<bool, AccLoginModel, string> retTuple = AccountBiz.Instance.Bind(mobile, code, openid, accesstoken, apiContext.apiHttpReq.machineCode);
            if (retTuple.Item1)
            {
                apiResponse.retCode = ResultEnum.Success;
                apiResponse.data = JsonHelper.Serialize(retTuple.Item2);
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = retTuple.Item3;
            }
            apiContext.apiHttpResp = apiResponse;
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        public void LoginOut(ApiServiceContext apiContext)
        {
            ApiHttpResponse apiResponse = new ApiHttpResponse();

            if (AccountBiz.Instance.LoginOut(apiContext.CurrUserId, apiContext.apiHttpReq.machineCode))
            {
                apiResponse.retCode = ResultEnum.Success;
            }
            else
            {
                apiResponse.retCode = ResultEnum.Fail;
                apiResponse.message = "退出登录失败";
            }
            apiContext.apiHttpResp = apiResponse;
        }
    }
}